﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/10 15:39:55
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;

    /// <summary>
    /// 用户查询组合类。
    /// </summary>
    public class UserQuery
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Common.UserQuery"/>。
        /// </summary>
        public UserQuery()
        {
            this.OrderBy = "UserID";
            PageSize = SecurityApplication.Current.Configuration.UserPageSize;
        }
        /// <summary>
        /// 获取或设置用户名称。
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 获取或设置是否精确查找。
        /// </summary>
        public bool IsExact { get; set; }
        /// <summary>
        /// 获取或设置电子邮件地址。
        /// </summary>
        public string Email { get; set; }
        /// <summary>
        /// 获取或设置角色ID集合。
        /// </summary>
        public string RoleIDs { get; set; }
        /// <summary>
        /// 获取或设置创建IP。
        /// </summary>
        public string CreateIP { get; set; }
        /// <summary>
        /// 获取或设置最后登录IP。
        /// </summary>
        public string LastLoginIP { get; set; }
        /// <summary>
        /// 获取或设置创建日期早于。
        /// </summary>
        public string CreateDateBefore { get; set; }
        /// <summary>
        /// 获取或设置创建日期晚于。
        /// </summary>
        public string CreateDateAfter { get; set; }
        /// <summary>
        /// 获取或设置最后登录日期早于。
        /// </summary>
        public string LastLoginDateBefore { get; set; }
        /// <summary>
        /// 获取或设置最后登录日期晚于。
        /// </summary>
        public string LastLoginDateAfter { get; set; }
        /// <summary>
        /// 获取或设置生日。
        /// </summary>
        public string BirthDate { get; set; }
        /// <summary>
        /// 获取或设置页码。
        /// </summary>
        public int PageIndex { get; set; }
        /// <summary>
        /// 获取或设置每页记录数。
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 获取或设置排序字符串。
        /// </summary>
        public string OrderBy { get; set; }
        /// <summary>
        /// 获取或设置是否为降序排列。
        /// </summary>
        public bool IsDesc { get; set; }
        /// <summary>
        /// 获取条件字符串。
        /// </summary>
        /// <param name="func">表格前缀函数代理。</param>
        /// <param name="fromSet">返回数据表格字符串。</param>
        /// <returns>返回条件字符串。</returns>
        public virtual string GetWhere(Func<string, string> func ,out string fromSet)
        {
            StringBuilder sb = new StringBuilder();
            List<string> froms = new List<string>();
            froms.Add(func("Users") + " u");
            sb.Append(" IsAnonymous = 0");
            if (!string.IsNullOrEmpty(this.UserName))
            {
                sb.Append(" AND");
                if (this.IsExact)
                    sb.AppendFormat(" u.UserName = '{0}'", this.UserName.SQLEncode());
                else
                    sb.AppendFormat(" u.UserName LIKE '%{0}%'", this.UserName.SQLEncode());
            }
            if (!string.IsNullOrEmpty(this.Email))
            {
                sb.AppendFormat(" AND u.Email = '{0}'", this.Email.SQLEncode());
            }
            if (!string.IsNullOrEmpty(this.CreateIP))
            {
                sb.AppendFormat(" AND u.CreateIP = '{0}'", this.CreateIP.SQLEncode());
            }
            if (!string.IsNullOrEmpty(this.LastLoginIP))
            {
                sb.AppendFormat(" AND u.LastLoginIP = '{0}'", this.LastLoginIP.SQLEncode());
            }
            if (HasDate(CreateDateBefore))
            {
                sb.Append(" AND");
                sb.AppendFormat(" u.CreateDate <= '{0}'", this.CreateDateBefore.ToDateTime());
            }
            if (HasDate(CreateDateAfter))
            {
                sb.AppendFormat(" AND u.CreateDate >= '{0}'", this.CreateDateAfter.ToDateTime());
            }
            if (HasDate(LastLoginDateBefore))
            {
                sb.AppendFormat(" AND u.LastLoginDate <= '{0}'", this.LastLoginDateBefore.ToDateTime());
            }
            if (HasDate(LastLoginDateAfter))
            {
                sb.AppendFormat(" AND u.LastLoginDate >= '{0}'", this.LastLoginDateAfter.ToDateTime());
            }
            if (HasDate(BirthDate))
            {
                DateTime birthDate = BirthDate.ToDateTime();
                sb.AppendFormat(" AND u.BirthYear = {0} AND u.BirthMonth = {1} AND u.BirthDay = {2}", birthDate.Year, birthDate.Month, birthDate.Day);
            }
            if (!string.IsNullOrEmpty(RoleIDs))
            {
                sb.AppendFormat(" AND ur.RoleID IN ({0})", this.RoleIDs);
                froms.Add("INNER JOIN "+func("UserInRoles")+" ur ON u.UserID = ur.UserID");
            }
            fromSet = froms.Join("\r\n");
            return sb.ToString();
        }
        /// <summary>
        /// 获取是否为空。
        /// </summary>
        public bool IsNull
        {
            get
            {
                return string.IsNullOrEmpty(this.UserName) &&
                    string.IsNullOrEmpty(this.Email) &&
                    string.IsNullOrEmpty(this.CreateIP) &&
                    string.IsNullOrEmpty(this.LastLoginIP) &&
                    !HasDate(CreateDateBefore) &&
                    !HasDate(CreateDateAfter) &&
                    !HasDate(LastLoginDateBefore) &&
                    !HasDate(LastLoginDateAfter) &&
                    !HasDate(BirthDate) &&
                    string.IsNullOrEmpty(RoleIDs);
            }
        }
        private bool HasDate(string date)
        {
            return !string.IsNullOrEmpty(date) && date.IsDate();
        }
        /// <summary>
        /// 从QueryString获取查询对象。
        /// </summary>
        public static UserQuery Current
        {
            get
            {
                UserQuery query = new UserQuery();
                AppContext context = AppContext.Current;
                query.BirthDate = context.QueryString["bd"];
                query.CreateDateAfter = context.QueryString["ca"];
                query.CreateDateBefore = context.QueryString["cb"];
                query.CreateIP = context.QueryString["ci"];
                query.Email = context.QueryString["e"];
                query.IsDesc = context.QueryString["id"].ToBoolean();
                query.IsExact = context.QueryString["ie"].ToBoolean();
                query.LastLoginDateAfter = context.QueryString["la"];
                query.LastLoginDateBefore = context.QueryString["lb"];
                query.LastLoginIP = context.QueryString["li"];
                query.OrderBy = context.QueryString["o"] ?? "UserID";
                query.PageIndex = context.PageIndex - 1;
                query.PageSize = SecurityApplication.Current.Configuration.UserPageSize;
                query.RoleIDs = context.QueryString["ri"];
                query.UserName = context.QueryString["u"];
                return query;
            }
        }
    }
}